define(["app",
        "jquery",
        "services/UserService",
        "services/MenuService",
        "directives/showtimes/UpdatePassword",
        "directives/yoki/AddCity",
        "services/WindowActivatedService",
        "resources/UserResource",
        "directives/Loading",
        "resources/company/CompanyResource"
], function(app, $) {

    "use strict";
	
	var deps = ["$scope", "$state", "UserService", "UserResource", "$location", "MenuService", "WindowActivatedService","CompanyResource"];
	
	function controller($scope, $state, UserService, UserResource, $location, MenuService, WindowActivatedService,CompanyResource) {

		
		$scope.user = UserService.getCurrentUser();
		
		$scope.menuConfig = MenuService.companyConfig;

        //刷新页面初始化时，确定当前菜单
		$scope.currentMenuItem = MenuService.queryByLocationAdminURL($location.$$url);
        
		$scope.mobile=$scope.user.mobile;
        
        //菜单跳转时，更新当前菜单项
        $scope.refreshCurMenu = function(menu) {
            $scope.currentMenuItem = $scope.menuConfig[menu.markName];
            if (menu.markName === "accountManagement") {
                $scope.showChatMark = false;
            }
        };
        
        $scope.userId=$scope.user.userId;

        /*$scope.$on('companyAddBroadCast', function($event, data) {
        	$event.stopPropagation();
        	$scope.$broadcast('companyFBroadCast');
        });
        
        
        function setBroadCast(){
        	$scope.$broadcast('companyFBroadCast',function(event, data){
    	        $scope.$broadcast("event.upload.display", attachFiles);
            });
        }*/
        
      //var currentUser =UserService.getCurrentUser();
		/*$scope.changeLogoutImage =function (flag){
			var image =document.getElementById('imageChanges');
			if(flag){
				image.src="images/logout2.png";
			}else{
				image.src="images/logout1.png";
			}
		};*/
		
        $scope.$on('loginoutBroadCast', function($event, data) {
        	$event.stopPropagation();
        	UserResource.logout(function(){
				$scope.user = UserService.getCurrentUser();
				UserService.removeCurrentUser();
				$location.path("/login");
			});
        });
        
        //控制指定的一级菜单展开，其他一级菜单下的二级菜单收起
        function setSubMenuExpand(firstMenuName,time){
            if(firstMenuName){
               var firstLMenu = MenuService.firstLevelMenus;
               for(var i in firstLMenu){
                   if(firstLMenu[i] == firstMenuName){
                       controlSubMenuExpand(firstLMenu[i],true,time||0);
                   }else{
                       controlSubMenuExpand(firstLMenu[i],false,time||0);
                   }
               }
            }
        }

        //按照菜单名控制单个二级菜单展开或收起
        function controlSubMenuExpand(menuMarkName,expand,time){
            var $menuItem = $("#"+menuMarkName+"_menuItemA");
            if($menuItem.length>0){
                var $li = $menuItem.closest("li");
                var $sub = $li.find(".menu-second");
                var $img = $li.find(".menu-sidebar");
                if(expand){
                    $sub.slideDown(time);
                    $li.addClass("menu-collapse");
                    $img.attr("src","images/home/menu_icon_dropdown_normal.png");
                }else{
                    $sub.slideUp(time);
                    $li.removeClass("menu-collapse");
                    $img.attr("src","images/home/menu_icon_dropdown_focus.png");
                }
            }
        }

        //单击切换二级菜单展开收起
        $scope.toggleSub = function($event,menu){
            var $li = $($event.target).closest("li");
            if ($li.hasClass("menu-collapse")) {
                controlSubMenuExpand(menu.markName,false,300);
            } else {
                setSubMenuExpand(menu.markName,300);
            }
        };

		/*$scope.logout = function() {
			UserResource.logout(function(){
				$scope.user = UserService.getCurrentUser();
				UserService.removeCurrentUser();
				$location.path("/login");
			});
		};*/
		$scope.logout = function() {
			CompanyResource.logout().success(function(data){
				$scope.user = UserService.getCurrentUser();
				UserService.removeCurrentUser();
				$location.path("/companylogin");
            });
		};
		
	}
	
	controller.$inject = deps;
	app.lazy.controller("HomeController", controller);
});
